//
//  View_VedioPlayer.m
//  yjw-winner
//
//  Created by 王春辉 on 2018/9/14.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "View_VedioPlayer.h"

@interface View_VedioPlayer ()<UIGestureRecognizerDelegate> {
    NSTimer *_timer;    // 隐藏工具栏计时器
}
@property (strong, nonatomic) NSArray *excludedViews; // 不响应手势的view
@property (nonatomic, setter=setHiddenToolBar:, getter=isHiddenToolBar) BOOL hiddenToolBar; // 是否隐藏工具栏
@end
@implementation View_VedioPlayer
+ (View_VedioPlayer *)vedioPlayerView {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self.progressSlider setThumbImage:[UIImage imageNamed:@"un_vedio_slider_thumb"] forState:UIControlStateNormal];
    [self.progressSlider setThumbImage:[UIImage imageNamed:@"un_vedio_slider_thumb"] forState:UIControlStateHighlighted];
    self.excludedViews = @[self.naviBarView, self.toolBarView];
    [self resetTimer];
    [self.toolBarView addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(vedioViewTapGesture:)];
    tap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tap];
    tap.delegate = self;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"hidden"]) {
        self.hiddenToolBar = [[change valueForKey:@"new"] intValue];
    }
}
- (void)dealloc {
    [self.toolBarView removeObserver:self forKeyPath:@"hidden"];
}


#pragma mark --mark UIGesctureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return ![self.excludedViews containsObject:touch.view] && ![self.excludedViews containsObject:touch.view.superview];
}

/**
 开启计时器
 */
- (void)resetTimer {
    [_timer invalidate];
    __weak typeof (self) weakself = self;
    if (@available(iOS 10.0, *)) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:5.0f repeats:NO block:^(NSTimer * _Nonnull timer) {
                typeof (weakself) strongself = weakself;
                [strongself timerSelector:timer];
        }];
    } else {
        // Fallback on earlier versions
        _timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO];
    }
}
- (void)timerSelector:(NSTimer *)timer {
    if (_timer.isValid) {
        [_timer invalidate];
        _timer = nil;
        [self hiddenToolBar];
    }
}
/**
 关闭计时器
 */
- (void)closeTimer {
    if ([_timer isValid]) {
        [_timer invalidate];
    }
    _timer = nil;
}
/**
 展示工具栏
 */
- (void)showToolBar {
    [UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self showTabBarView];
        [self showNavigationBarView];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)showTabBarView {
    if (![self.toolBarView isHidden]) {
        return;
    }
    self.toolBarView.hidden = NO;
    self.toolBarView.alpha = 1.0f;
}
/**
 隐藏工具栏
 */
- (void)hiddenToolBar {
    [UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self hiddenTabBarView];
        [self hiddenNavigationBarView];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)hiddenTabBarView {
    if ([self.toolBarView isHidden]) {
        return;
    }
    self.toolBarView.alpha = 0.0f;
    self.toolBarView.hidden = YES;
}

/**
 隐藏导航栏
 */
- (void)hiddenNavigationBarView {
    if ([self.naviBarView isHidden]) {
        return;
    }
    self.naviBarView.alpha = 0.0f;
    self.naviBarView.hidden = YES;
}

/**
 展示导航栏
 */
- (void)showNavigationBarView {
    if (![self.naviBarView isHidden]) {
        return;
    }
    self.naviBarView.hidden = NO;
    self.naviBarView.alpha = 1.0f;
}
#pragma mark --mark EVENT

/**
 页面点击收拾

 @param sender sender
 */
- (void)vedioViewTapGesture:(UITapGestureRecognizer *)sender {
    if (self.hiddenToolBar) {
        // 隐藏状态
        [self showToolBar];
        [self resetTimer];
    }else {
        [self hiddenToolBar];
        [self closeTimer];
    }
}

/**
 播放暂停按钮

 @param sender sender
 */
- (IBAction)vedioPlayButtonClick:(UIButton *)sender {
    
}

/**
 是否全凭按钮

 @param sender sender
 */
- (IBAction)vedioScreenButtonClick:(UIButton *)sender {
    
}

/**
 按下 slider thumb

 @param sender sender
 */
- (IBAction)vedioSliderTouchStart:(UISlider *)sender {
    [self closeTimer];
}

/**
 滑动slider

 @param sender sender
 */
- (IBAction)vedioSliderValueChanged:(UISlider *)sender {
    
}

/**
 松开slider按钮

 @param sender sender
 */
- (IBAction)vedioSliderTouchEnd:(UISlider *)sender {
    [self resetTimer];
}

#pragma mark --mark 业务逻辑
/**
 设置背景图片
 
 @param image image
 */
- (void)vedioSetBackGoundImage:(UIImage *)image {
    
}

/**
 设置播放按钮状态
 
 @param isPause YES/NO
 */
- (void)vedioSetPlayButtonStatus:(BOOL)isPause {
    
}

/**
 设置播放进度
 
 @param progress progress
 */
- (void)vedioSetProgressLabelWith:(NSString *)progress {
    
}

/**
 设置slider value
 
 @param progress progress
 */
- (void)vedioSetSliderProgressValue:(CGFloat)progress {
    
}

/**
 设置总时长
 
 @param progress progress
 */
- (void)vedioSetTotalProgressValue:(CGFloat)progress {
    
}

@end
